﻿using GalaSoft.MvvmLight.Messaging;
using System;
using System.Threading;
using System.Windows;
using TransControlSystem.ViewModel;

namespace TransControlSystem.View
{
    /// <summary>
    /// TaskQueryWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TaskQueryWindow : Window
    {
        private TaskQueryViewModel taskQueryViewModel = null;
        public TaskQueryWindow()
        {
            InitializeComponent();
            #region 全屏效果代码
            WindowState = System.Windows.WindowState.Maximized;//还原窗口（非最小化和最大化）
            ResizeMode = System.Windows.ResizeMode.CanResizeWithGrip;//不显示最大化和最小化按钮
            Topmost = false;    //窗口在最前
            Left = 0.0;
            Top = 0.0;
            Width = System.Windows.SystemParameters.PrimaryScreenWidth;
            Height = System.Windows.SystemParameters.PrimaryScreenHeight;
            #endregion
            Messenger.Default.Register<NotificationMessage>(this, (nm) =>
            {
                if (nm.Notification == "HideTaskQueryWindow")
                {
                    if (nm.Sender == DataContext)
                        Hide();
                }
            });
            txt_TaskDesc.Text = "";
            txt_StartTime.DateTime = Convert.ToDateTime("0001-01-01 00:00:00");
            txt_EndTime.DateTime = Convert.ToDateTime("0001-01-01 00:00:00");
            taskQueryViewModel = (TaskQueryViewModel)DataContext;
            Thread SynchronizePropertyValuesThread = new Thread(new ThreadStart(SynchronizePropertyValues))
            {
                IsBackground = true,
                Priority = ThreadPriority.AboveNormal,
#pragma warning disable CS0618 // “Thread.ApartmentState”已过时:“The ApartmentState property has been deprecated.  Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.”
                ApartmentState = ApartmentState.STA
#pragma warning restore CS0618 // “Thread.ApartmentState”已过时:“The ApartmentState property has been deprecated.  Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.”
            };
            SynchronizePropertyValuesThread.Start();
        }
        /// <summary>
        /// 同步时间
        /// </summary>
        private void SynchronizePropertyValues()
        {
            while (true)
            {
                if (txt_StartTime.DateTime != Convert.ToDateTime("0001-01-01 00:00:00"))
                {
                    taskQueryViewModel.StartTime = txt_StartTime.DateTime.ToString();
                }

                if (txt_EndTime.DateTime != Convert.ToDateTime("0001-01-01 00:00:00"))
                {
                    taskQueryViewModel.EndTime = txt_EndTime.DateTime.ToString();
                }
                Thread.Sleep(10);
            }
        }

        private void DataGrid_LoadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }
    }
}
